﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using web.data.entities;
using System.Configuration;
using Webdiyer.WebControls.Mvc;
using System.Web.UI;
using System.Linq.Expressions;
using System.Reflection;
using System.Xml.Schema;
using web.component;

namespace web.bussines
{
    public class page_static_service : base_service
    {
        //get all get all
        public PagedList<page_static> adm_page_static_list(int pageindex, int pagesize, int language_id)
        {
            using (var data = new baseEntities())
            {
                var list = (from dt in data.tbl_page_static
                            where dt.language_id == language_id
                            //orderby dt.page_id ascending
                            select dt);
                list = list.OrderBy(p => p.order_display);
                return list.ToPagedList(pageindex, pagesize);
            }
        }

        public List<page_static> page_static_list_by_status(bool isshow, bool onmenu, int language_id)
        {
            using (var data = new baseEntities())
            {
                var list = (from dt in data.tbl_page_static
                            where dt.show == isshow &&
                                    dt.language_id == language_id &&
                                    dt.on_menu == onmenu
                            select dt);
                list = list.OrderBy(p => p.order_display);
                return list.ToList<page_static>();
            }
        }

        public int adm_page_static_update(page_static it)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_page_static.Where(p => p.page_id == it.page_id).SingleOrDefault();
                if (item != null)
                {
                    item.page_id = it.page_id;
                    item.page_name = it.page_name;
                    item.link = it.link;
                    item.image_path = it.image_path;
                    item.summary = it.summary;
                    item.page_content = it.page_content;
                    item.date_modified = Udate_time.GetCurrentDateTime();// it.date_modified;
                    item.show = it.show;
                    item.language_id = it.language_id;
                    item.keyword = it.keyword;
                    item.description = it.description;
                    item.title = it.title;
                    item.order_display = it.order_display;
                    item.on_menu = it.on_menu;

                    data.SaveChanges();
                    return item.page_id;
                }
                else
                {
                    data.AddTotbl_page_static(it);
                    data.SaveChanges();
                    return it.page_id;
                }
            }
        }
        //enable - disable  
        public int adm_page_statis_enable(int page_id, bool is_show)
        {
            var rt = 0;
            using (var data = new baseEntities())
            {
                var edit_item = data.tbl_page_static.Where(p => p.page_id == page_id).SingleOrDefault();

                if (edit_item != null) {
                    edit_item.show = is_show;
                    rt = adm_page_static_update(edit_item);
                }
            } return rt;
        }
        //hien thi tren menu hoac ko
        public int adm_page_statis_on_menu(int page_id, bool on_menu)
        {
            var rt = 0;
            using (var data = new baseEntities())
            {
                var edit_item = data.tbl_page_static.Where(p => p.page_id == page_id).SingleOrDefault();

                if (edit_item != null)
                {
                    edit_item.on_menu = on_menu;
                    rt = adm_page_static_update(edit_item);
                }
            } return rt;
        }

        //get items by id or code
        public page_static getByID(int id)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_page_static.Where(p => p.page_id == id).FirstOrDefault();
               // if (item == null)
               //     item = new page_static();
                return item;
            }
        }

        //delete by id
        public void deletePage(int id)
        {
            using (var data = new baseEntities())
            {
                var em = data.tbl_page_static.Where(p => p.page_id == id).FirstOrDefault();

                if (em != null)
                {
                    data.DeleteObject(em);
                    data.SaveChanges();
                }
            }
        }

    }
}
